package com.jwds.digital.bios.business.service.service.basic.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwds.digital.bios.business.core.exceptions.RecordNotFoundException;
import com.jwds.digital.bios.business.core.infrastructure.dto.basic.BasicDictReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.basic.BasicOperateLogReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.basic.assembler.BasicDictAssembler;
import com.jwds.digital.bios.business.core.infrastructure.dto.basic.assembler.BasicOperateLogAssembler;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.assembler.BasicPowerTypeAssembler;
import com.jwds.digital.bios.business.core.infrastructure.entity.basic.BasicDict;
import com.jwds.digital.bios.business.core.infrastructure.entity.basic.BasicOperateLog;
import com.jwds.digital.bios.business.core.infrastructure.entity.belt.BasicPowerType;
import com.jwds.digital.bios.business.core.infrastructure.mapper.basic.BasicDictMapper;
import com.jwds.digital.bios.business.core.infrastructure.vo.basic.BasicDictRespVO;
import com.jwds.digital.bios.business.service.service.basic.IBasicDictService;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description BasicDictServiceImpl
 * @Author xiao  xun
 * @Date 2024-12-07
 */
@Slf4j
@Service
public class BasicDictServiceImpl extends ServiceImpl<BasicDictMapper, BasicDict> implements IBasicDictService {
    @Resource
    private BasicDictMapper basicDictMapper;

    @Override
    public PageResponse<BasicDictRespVO> page(PageRequest<BasicDictReqDTO> request) {
        BasicDictReqDTO condition = request.getQueryCondition();
        Page<BasicDictRespVO> page = new Page<>(request.getPageIndex(), request.getPageSize());
        IPage<BasicDictRespVO> pageData = basicDictMapper.pageQuery(page, condition);
        return new PageResponse<>((Page<?>) pageData, pageData.getRecords());
    }

    @Override
    public List<Tree<String>> getTree() {
        // 获取所有未删除的字典项
        List<BasicDictRespVO> allDicts = basicDictMapper.list();

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("itemCode");
        treeNodeConfig.setParentIdKey("parentCode");
        treeNodeConfig.setNameKey("itemName");
        treeNodeConfig.setDeep(3);
        treeNodeConfig.setChildrenKey("children");
        treeNodeConfig.setWeightKey("sort");

        //构建树
        List<Tree<String>> treeList = TreeUtil.build(allDicts, "0", treeNodeConfig, ((object, treeNode) -> {
            treeNode.putExtra("itemCode", object.getItemCode());
            treeNode.putExtra("parentCode", object.getParentCode());
            treeNode.putExtra("dataCategoryName", object.getItemName());
            treeNode.putExtra("sort", object.getSort());
            treeNode.putExtra("remark", object.getRemark());
        }));

        return treeList;
    }

    @Override
    public void update(BasicDictReqDTO reqDto) {
        BasicDict basicDict = basicDictMapper.selectById(reqDto.getId());
        if (basicDict == null) {
            throw new RecordNotFoundException();
        }
        BasicDict newEntity = BasicDictAssembler.dto2entity(reqDto, basicDict);
        basicDictMapper.updateById(newEntity);
    }

    @Override
    public void delete(Long id) {
        BasicDict basicDict = basicDictMapper.selectById(id);
        if (basicDict == null) {
            throw new RecordNotFoundException();
        }
        basicDictMapper.deleteById(id);
    }

    @Override
    public void add(BasicDictReqDTO reqDto) {
        BasicDict entity = BasicDictAssembler.dto2entity(reqDto);
        baseMapper.insert(entity);
    }

}
